
#include "../common/dispacher.hpp"
#include "../client/requestor.hpp"
#include "../client/rpc_caller.hpp"
#include <thread>
// 客户端服务端的测试1
//  void onRpcResponse(const bitpr::BaseConnection::ptr& conn,bitpr::RpcResponse::ptr &msg){

//     std::cout<<"响应Rpc"<<std::endl;
//     std::string body=msg->serialize();
//     std::cout<<body<<std::endl;

// }
// void onTopicResponse(const bitpr::BaseConnection::ptr& conn,bitpr::TopicResponse::ptr &msg){

//     std::cout<<"响应Topic"<<std::endl;
//     std::string body=msg->serialize();
//     std::cout<<body<<std::endl;

// }
void callback(const Json::Value& result)
{
    ELOG("result:%d",result.isInt());
    std::cout<<result.isInt()<<std::endl;
}
int main()
{

    auto requestor = std::make_shared<bitpr::client::Requestor>();
    auto caller = std::make_shared<bitpr::client::RpcCaller>(requestor); // 只写了一个构造函数

    auto dispacher = std::make_shared<bitpr::Dispacher>();
    // 注册映射关系
    auto rsp_cb = std::bind(&bitpr::client::Requestor::onResponse, requestor.get(),
                            std::placeholders::_1, std::placeholders::_2);

    dispacher->registerHandler<bitpr::BaseMessage>(bitpr::MType::RSP_RPC, rsp_cb);
    // // dispacher->registerHandler<bitpr::RpcResponse>(bitpr::MType::RSP_RPC,onRpcResponse);
    // // dispacher->registerHandler<bitpr::TopicResponse>(bitpr::MType::RSP_TOPIC,onTopicResponse);
    auto client = bitpr::ClientFactory::create("127.0.0.1", 9090);
    auto message_cb = std::bind(&bitpr::Dispacher::onMessage, dispacher.get(),
                                std::placeholders::_1, std::placeholders::_2);
    client->setMessageCallback(message_cb);
    client->connect();

    // 给出请求的具体方法参数
    std::cout << client->connected() << std::endl;
    auto conn = client->connection();
    if (conn == nullptr)
    {
        printf("conn为空");
    }

    Json::Value params, result;
    params["num1"] = 11;
    params["num2"] = 22;
    bool ret = caller->call(conn, "Add", params, result);
    if (ret != false)
    {
        std::cout << "result: " << result.asInt() << std::endl;
    }

    bitpr::client::RpcCaller::JsonAsyncResponse res_future;
    params["num1"] = 33;
    params["num2"] = 55;
    ret = caller->call(conn, "Add", params, res_future);
    if (ret != false)
    {
        result = res_future.get();
        std::cout << "result: " << result.asInt() << std::endl;
    }

    params["num1"] = 2;
    params["num2"] = 22;
    ret = caller->call(conn, "Add", params, callback);
    DLOG("---------");
   std::this_thread::sleep_for(std::chrono::seconds(1));
    // auto rpc_req=bitpr::MessageFactory::create<bitpr::RpcRequest>();
    // rpc_req->setId("111");
    // rpc_req->setMethod("Add");
    // Json::Value param;
    // param["num1"]=11;
    // param["num2"]=22;

    // rpc_req->setParams(param);
    // client->send(rpc_req);

    // auto topic_req=bitpr::MessageFactory::create<bitpr::TopicRequest>();
    // topic_req->setId("222");
    // topic_req->setMType(bitpr::MType::REQ_TOPIC);
    // topic_req->setTopicOptype(bitpr::TopicOptype::TTOPIC_CREATE);
    // topic_req->setTopicKey("news");
    // client->send(topic_req);

    // std::this_thread::sleep_for(std::chrono::seconds(10));
    client->shutdown();

    return 0;
}